<template>
  <div class="questions-box">
    <div class="questions-left">
      <paper-cate
        ref="paperCate"
        :setCurrent="setCurrent"
        :current="current"
        :url="{
          cateType: 2,
          update:'/exam-admin/ques/cate/update',
          save:'/exam-admin/ques/cate/save',
          delete:'/exam-admin/ques/cate/delete',
          tree:'/exam-admin/ques/cate/tree'
          }"
      />
    </div>
    <div class="questions-right" ref="fixedMain">
      <el-form
        :inline="true"
        :model="dataForm"
        @keyup.enter.native="getDataList(1)"
        ref="fixedForm"
      >
        <el-form-item>
          <el-input v-model="dataForm.key" placeholder="题干" clearable @clear="getDataList(1)"/>
        </el-form-item>
        <el-form-item>
          <!--          <el-input v-model="dataForm.quesLevel" placeholder="题型难度" clearable @clear="getDataList(1)"></el-input>-->
          <el-select
            v-model="dataForm.quesLevel"
            filterable
            placeholder="请选择"
            clearable
            @clear="getDataList(1)"
          >
            <el-option
              v-for="item in options"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            />
          </el-select>
        </el-form-item>
        <el-form-item>
          <el-button @click="getDataList(1)">查询</el-button>
          <el-button
            v-if="isAuth('questions:quessingerchoiceqst:save')"
            type="primary"
            @click="addOrUpdateHandle()"
          >新增
          </el-button>
          <el-button type="primary" @click="batchImportHandle()">批量导入</el-button>
          <el-button
            type="primary"
            @click="duplicatechecking('/exam-admin/ques/fillblacks/checking')"
          >查重
          </el-button>
          <el-button
            v-if="isAuth('questions:quessingerchoiceqst:delete')"
            type="danger"
            @click="deleteHandle()"
            :disabled="dataListSelections.length <= 0"
          >批量删除
          </el-button>
        </el-form-item>
      </el-form>

      <el-table
        :data="dataList"
        border
        v-loading="dataListLoading"
        @selection-change="selectionChangeHandle"
        width="100%"
        :height="fixedTableHeight"
      >
        <el-table-column
          fixed="left"
          type="selection"
          header-align="center"
          align="center"
          width="50"
        />
        <el-table-column type="index" header-align="center" align="center" label="序号" width="50"/>
        <el-table-column prop="cateName" header-align="center" align="center" label="试题分类"/>
        <el-table-column header-align="center" align="center" width="340" label="题干内容">
          <template slot-scope="scope">
            <span v-html="decode(scope.row.info)"/>
          </template>
        </el-table-column>
        <el-table-column header-align="center" align="center" label="答案">
          <template slot-scope="scope">
            <div>{{scope.row.answ | parseAnsw}}</div>
          </template>
        </el-table-column>
        <el-table-column
          prop="level"
          header-align="center"
          align="center"
          label="试题难度"
          :formatter="formatterLevel"
        />
        <el-table-column header-align="center" align="center" label="答案解析">
          <template slot-scope="scope">
            <div v-html="scope.row.analysis"></div>
          </template>
        </el-table-column>
        <el-table-column fixed="right" header-align="center" align="center" width="150" label="操作">
          <template slot-scope="scope">
            <el-button type="text" @click="addOrUpdateHandle(scope.row.id)">修改</el-button>
            <el-button type="text" @click="deleteHandle(scope.row.id)">删除</el-button>
          </template>
        </el-table-column>
      </el-table>
      <el-pagination
        ref="fixedPag"
        @size-change="sizeChangeHandle"
        @current-change="currentChangeHandle"
        :current-page="pageIndex"
        :page-sizes="[10, 20, 50]"
        :page-size="pageSize"
        :total="totalPage"
        layout="total, sizes, prev, pager, next, jumper"
      />
      <!-- 弹窗, 新增 / 修改 -->
      <add-or-update ref="addOrUpdate" @refreshDataList="getDataList"/>
      <batch-import
        v-if="batchImportVisible"
        ref="batchImport"
        url="/exam-admin/ques/import/fillBlacks"
        @uploadFile="uploadFile"
      />
      <!--      url="/exam-admin/ques/import/fillblacks"-->
      <check-table v-if="CheckVisible" ref="checkTable" :getDataList="getDataList"/>
      <!-- 分类管理 -->
    </div>
  </div>
</template>


<script>
  import paperCate from './../../../views/modules/paper/papercate'
  import AddOrUpdate from './fill-blank-add-or-update'
  import BatchImport from './batchimport'
  import CheckTable from '../exam/checkTable'
  import {decodeSrc} from '@/utils/htmlUtils'

  this.pageIndex > 1 && this.dataList.length <= 1 && this.pageIndex--
  export default {
    data() {
      return {
        fixedTableHeight: 0,
        dialogFormVisible: false,
        current: {},
        paperCate: false,
        CheckVisible: false,
        dataForm: {
          key: '',
          quesCateId: '',
          quesLevel: ''
        },
        options: [
          {
            value: '0',
            label: '简单'
          },
          {
            value: '1',
            label: '正常'
          },
          {
            value: '2',
            label: '困难'
          }
        ],
        loading: null,
        dataList: [],
        balantList: [],
        quesType: 1,
        pageIndex: 1,
        pageSize: 10,
        totalPage: 0,
        answs: '',
        answList: [],
        answer: '',
        dataListLoading: false,
        dataListSelections: [],
        batchImportVisible: false
      }
    },
    components: {
      paperCate,
      CheckTable,
      AddOrUpdate,
      BatchImport
    },
    mounted() {
      this.$refs.paperCate.init()
      this.getDataList(1)
      this.$nextTick(_ => {
        this.examResize()
        window.addEventListener('resize', () => this.examResize())
      })
    },
    filters: {
      parseAnsw(answ) {
        let answList = JSON.parse(answ)
        let answStr = ''
        if (answList != null && answList.length > 0) {
          for (let i = 0; i < answList.length; i++) {
            for (let j = 0; j < answList[i].length; j++) {
              if (j < answList[i].length - 1) {
                answStr += answList[i][j] + ' || '
              } else {
                answStr += answList[i][j]
              }
            }
            if (i < answList.length - 1) {
              answStr += ' && '
            }
          }
        }
        return answStr
      }
    },
    methods: {
      examResize() {
        if (this.$refs.fixedMain) {
          this.fixedTableHeight =
            this.$refs.fixedMain.clientHeight -
            this.$refs.fixedForm.$el.offsetHeight -
            this.$refs.fixedPag.$el.offsetHeight -
            15
        }
      },
      decode(val) {
        return decodeSrc(val)
      },
      setCurrent(data) {
        this.current = data
        this.dataForm.quesCateId = data.id
        this.getDataList(1)
      },
      batchImportHandle() {
        this.batchImportVisible = true
        this.$nextTick(() => {
          this.$refs.batchImport.init(-1)
        })
      },
      duplicatechecking(url) {
        this.CheckVisible = true
        this.$nextTick(() => {
          this.$refs.checkTable.init(url, 1, this.quesType)
        })
      },
      // 获取数据列表
      getDataList(page) {
        if (page) this.pageIndex = page
        this.dataListLoading = true
        this.$http({
          url: this.$http.adornUrl('/exam-admin/ques/fillblacks/list'),
          method: 'post',
          data: {
            page: this.pageIndex,
            limit: this.pageSize,
            keyword: this.dataForm.key,
            quesCateId: this.dataForm.quesCateId,
            quesLevel: this.dataForm.quesLevel
          }
        }).then(data => {
          if (data && data.code === 200) {
            this.dataList = data.data.list
            this.totalPage = data.data.totalCount
          } else {
            this.dataList = []
            this.totalPage = 0
          }
          this.dataListLoading = false
        })
      },
      // 每页数
      sizeChangeHandle(val) {
        this.pageSize = val
        this.pageIndex = 1
        this.getDataList()
      },
      // 当前页
      currentChangeHandle(val) {
        this.pageIndex = val
        this.getDataList()
      },
      // 多选
      selectionChangeHandle(val) {
        this.dataListSelections = val
      },
      // 新增 / 修改
      addOrUpdateHandle(id) {
        this.$refs.addOrUpdate.open(id)
      },
      // 删除
      deleteHandle(id) {
        this.$confirm(`确定要进行删除操作吗？`, '提示', {
          confirmButtonText: '确定',
          cancelButtonText: '取消',
          type: 'warning'
        }).then(() => {
          this.$http({
            url: this.$http.adornUrl('/exam-admin/ques/fillblacks/delete'),
            method: 'post',
            data: {
              ids: id || this.dataListSelections.map(item => item.id).join(',')
            }
          }).then(data => {
            if (data && data.code === 200) {
              this.$message({
                message: '操作成功',
                type: 'success',
                duration: 1500
              })
              this.pageIndex > 1 && this.dataList.length <= 1 && this.pageIndex--
              this.getDataList()
            } else {
              this.$message.error(data.msg)
            }
          })
        })
      },
      formatterLevel(row, column) {
        switch (row.level) {
          case 0:
            return '简单'
          case 1:
            return '正常'
          case 2:
            return '困难'
        }
      },
      uploadFile(data) {
        this.loading = this.$loading({
          lock: true,
          text: 'Loading',
          spinner: 'el-icon-loading',
          background: 'rgba(255, 255, 255, 0.7)'
        })
        this.$http({
          url: this.$http.adornUrl('/exam-admin/ques/import/save'),
          method: 'post',
          data: data
        }).then(res => {
          if (parseInt(res.code) === 200) {
            this.$notify.success('导入成功')
            this.getDataList()
            this.$refs.paperCate.init()
            this.batchImportVisible = false
          } else {
            this.$notify.error(res.msg)
            this.$refs.batchImport.resetFile()
          }
          this.loading.close()
        })
      }
    },
    destroyed() {
      window.removeEventListener('resize', this.examResize())
    }
  }
</script>
<style lang="scss" scoped>
  .questions-box {
    height: 100%;
    display: flex;

    .questions-left {
      padding-right: 10px;
      border-right: 1px solid #9e9e9e;
    }

    .questions-right {
      flex: 1;
      padding-left: 10px;
      overflow: auto;
    }
  }
</style>
